home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / gopher / Unix / GopherTools / gopher2mbox.Z / gopher2mbox
Encoding:
Text File  |  1994-05-03  |  4.5 KB  |  210 lines

  1. #!/usr/bin/perl
  2. # !/local/all/perl
  3.  
  4. # History
  5. #  Dec 6           cmk - modifed input a bookmark to a directory
  6. #  Nov 23-30, 1992 cmk - initial development
  7.  
  8. require "getopts.pl";
  9.  
  10. &Getopts("p:l:");
  11.  
  12. $toppath = $opt_p;
  13.  
  14. #print STDERR "opt_l=$opt_l\n";
  15. if ($opt_l ne '') {
  16.     $line_limit = int($opt_l);
  17.     if ($line_limit < 0) {$line_limit=0;}
  18. } else {
  19.     $line_limit = 200;
  20. }    
  21.  
  22. ($tophost,$topport) = @ARGV;
  23. $topport = 70 if ! $topport;
  24.  
  25. if (! $tophost) {
  26.     print "USAGE: gopher2mbox -pGOPHER-PATH -lLINE-LIMIT HOST PORT > MBOX\n";
  27.     print "\n";
  28.     print "    HOST is required\n";
  29.     print "    PORT defaults to 70\n";
  30.     print "    GOPHER-PATH defaults to \"\"\n";
  31.     print "    LINE-LIMIT defaults to 200\n";
  32.     print "\n";
  33.     print " MBOX can be read with a text editor, or with\n";
  34.     print "     nn MBOX\n";
  35.     print " or with\n";
  36.     print "     mail -f MBOX\n";
  37.     exit(1);
  38.     }
  39.  
  40. $| = 1;
  41.  
  42. local(@cltmp3) = &CLIENT($tophost,$topport,"$toppath\r\n",-1);
  43. #print STDERR ">5.0>$name,$type,$port,$path,$host<1<\n";
  44. ($lines,$count) = @cltmp3;
  45. #print STDERR ">5.1>$name,$type,$port,$path,$host<1<\n";
  46. #print "lines = $lines\n";
  47. foreach $_ (split("\n",$lines)) {
  48.     #print "_ = $_\n";
  49.     #chop; # chop off \r
  50.     &LOOKUP($_);
  51.     }
  52. #print STDERR ">5.2>$name,$type,$port,$path,$host<1<\n";
  53. # Based on "client" on page 344 of _Programming Perl_ by Wall and Schwartz
  54.  
  55. sub CLIENT {
  56.  
  57. local($them,$port,$input,$maxlines) = @_;
  58. local($client,$name);
  59.  
  60. if (($them eq "error.host") || ($maxlines==0)) {
  61.     $client="";$maxlines = 0;
  62.      local(@cltmp2) = ($client,$maxlines);
  63.     return(@cltmp2);
  64.     }
  65.  
  66. # If $maxlines is negative, then no limit.
  67.  
  68. #open(OUT,$fileout);
  69.  
  70. $port = 2345 unless $port;
  71. $them = 'localhost' unless $them;
  72.  
  73. $AF_INET =2;
  74. $SOCK_STREAM = 1;
  75.  
  76. $SIG{'INT'} = 'dokill';
  77.  
  78. $sockaddr = 'S n a4 x8';
  79.  
  80. chop($hostname = `hostname`);
  81.  
  82. ($name,$aliases,$proto) = getprotobyname('tcp');
  83. ($name,$aliases,$port) = getservbyname($port,'tcp')
  84.      unless $port =~ /^\d+$/;;
  85. ($name,$aliases,$type1,$len,$thisaddr) = gethostbyname($hostname);
  86. ($name,$aliases,$type2,$len,$thataddr) = gethostbyname($them);
  87.  
  88. $this = pack($sockaddr,$AF_INET, 0, $thisaddr);
  89. $that = pack($sockaddr,$AF_INET, $port, $thataddr);
  90.  
  91. # Make the socket filehandle
  92.  
  93. if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
  94.     #print "socket ok\n";
  95. }
  96. else {
  97.     die $!;
  98. }
  99.  
  100. # Give the socket an address.
  101.  
  102. if (bind(S, $this)) {
  103.     #print "bind ok\n";
  104. }
  105. else {
  106.     die $!;
  107. }
  108.  
  109. # Call up the server.
  110.  
  111. if (connect(S,$that)) {
  112.     #print "connect ok\n";
  113. }
  114. else {
  115.     print STDERR "CANNOT CONNECT TO $them $port $input $maxlines $!\n";
  116.     $client="";$maxlines = 0;
  117.      local(@cltmp2) = ($client,$maxlines);
  118.     return(@cltmp2);
  119. }
  120.  
  121. # Set socket to be command buffered.
  122.  
  123. $oldhandle = select(S); $| = 1; select ($oldhandle);
  124. #select(S); $| = 1; select (STDOUT);
  125.  
  126. # Avoid deadlock by forking.
  127.  
  128. #if (!($child = fork)) {
  129. #    # child
  130.     print S $input;
  131. #    exit;
  132. #}
  133. #else {
  134. #    # parent
  135.     READ: while (<S>) {
  136.         last READ if ($maxlines == 0);
  137.         $maxlines--;
  138.         #print OUT;
  139.         chop;chop;
  140.         last READ if ($_ eq ".");
  141.         $client .= "$_\n";
  142.     }
  143. #}
  144.  
  145. #close(OUT);
  146. local(@cltmp2) = ($client,$maxlines);
  147. @cltmp2;
  148. }
  149.  
  150. #sub dokill {
  151. #    kill 9,$child if $child;
  152. #}
  153.  
  154. sub LOOKUP {
  155.  
  156. local($gopherref) = @_;
  157. print "gr=$gopherref\n";
  158. $*=1;
  159. #print STDERR ">0>$gopherref<0<";
  160. local($type_and_name,$path,$host,$port) = split("\t",@_[0]);
  161. local($type,$name) = $type_and_name =~ /^(.)(.*)/;
  162. #print STDERR ">1>$name,$type,$port,$path,$host<1<\n";
  163.  
  164. $date = `date`;
  165. chop($date);
  166. #print STDERR $date;
  167. print "From gopher2mbox $date
  168. From: $port ($path)
  169. Subject: $name
  170. ";
  171. #print STDERR ">2>$name,$type,$port,$path,$host<1<\n";
  172. if ($type == 0) {
  173.     #print STDERR "line_limit = $line_limit\n";
  174.     #print STDERR ">3.0>$name,$type,$port,$path,$host<1<\n";
  175.     local(@cltmp3) = &CLIENT($host,$port,"$path\r\n",$line_limit);
  176.     #print STDERR ">3.1>$name,$type,$port,$path,$host<1<\n";
  177.     ($lines,$count) = @cltmp3;
  178.     #print STDERR ">3.2>$name,$type,$port,$path,$host<1<\n";
  179.     #print STDERR "count=$count\n";
  180.     print "\n$lines";
  181.     #print STDERR ">3.3>$name,$type,$port,$path,$host<1<\n";
  182.         if ($count ==0){
  183.         print "======== ... ================
  184.  
  185. [This is only the first part of this item. To retrieve the
  186. full item, use gopher.]
  187. ";
  188.     } else {
  189. print "======== End of \"$name\" ================\n";
  190.    }
  191.   } else {
  192. #print STDERR ">4>$name,$type,$port,$path,$host<1<\n";
  193. print "
  194. $name
  195. is a nontext item in Gopher. To access this item, try
  196.     gopher -p \"$path\" $host $port
  197. ";
  198. }
  199. #print STDERR ">5>$name,$type,$port,$path,$host<1<\n";
  200. print "For reference, the \"bookmark\" for this gopher item is:
  201.      Name=$name
  202.      Type=$type
  203.      Port=$port
  204.      Path=$path
  205.      Host=$host
  206.  
  207. ";
  208. #print STDERR ">1>$name,$type,$port,$path,$host<1<\n";
  209. }
  210.